import requests
import shutil
import os

root_path = f'./static/巨潮资讯'

if os.path.exists(root_path):
    shutil.rmtree(root_path)
os.makedirs(root_path)


for page in range(1, 8):
    res = requests.post('https://www.cninfo.com.cn/new/disclosure', data={
        "column": "szse_main_latest",
        "pageNum": page,
        "pageSize": 30,
        "sortName": "",
        "sortType": "",
        "clusterFlag": "true"
    })
    for items in res.json()['classifiedAnnouncements']:
        for item in items:
            url = f'https://static.cninfo.com.cn/{item["adjunctUrl"]}'
            company_path = f'{root_path}/{item["secName"]}'.replace("*", "星")
            if not os.path.exists(company_path):
                os.makedirs(company_path)

            res = requests.get(url)
            with open(f'{company_path}/{item["announcementTitle"].replace("*", "星")}.{item["adjunctType"]}',
                      'wb') as f:
                f.write(res.content)

# from selenium import webdriver
# from selenium.webdriver.chrome.service import Service
# from selenium.webdriver.common.by import By
# import time
# service = Service('webdriver/chromedriver-win64/chromedriver.exe')
# # 创建 WebDriver
# driver = webdriver.Chrome(service=service)
# driver.maximize_window()
# driver.get('https://www.cninfo.com.cn/new/commonUrl?url=disclosure/list/notice#szseMain%2Fimportant')
#
# while True:
#     items = driver.find_elements(By.XPATH, '//span[@class="ell tdatitle"]')
#     for item in items:
#         print(item.text)
#         # item.click()
#         # time.sleep(1)
#         # driver.switch_to.window(driver.window_handles[-1])
#         # download_btn = driver.find_element(By.XPATH, '//div[@class="nt-btns"]/button')
#         # download_btn.click()
#         # time.sleep(5)
#         #
#         # driver.close()
#         # time.sleep(1)
#         # driver.switch_to.window(driver.window_handles[0])
#
#     nex_page = driver.find_element(By.CLASS_NAME, 'btn-next')
#     can_click = nex_page.get_attribute("disabled")
#     if not can_click:
#         print(f"=================发现下一页=====================")
#         nex_page.click()
#         time.sleep(5)
#     else:
#         print(f"爬取结束")
#         break
#
#
#
# time.sleep(50)
# driver.quit()